package com.m3958.firstgwt.server.servlet;

import java.io.IOException;
import java.io.Writer;
import java.util.List;
import java.util.regex.Pattern;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import com.google.inject.persist.Transactional;
import com.m3958.firstgwt.server.dao.TagDao;
import com.m3958.firstgwt.server.model.Article;
import com.m3958.firstgwt.server.model.Section;
import com.m3958.firstgwt.server.model.Tag;


@Singleton
public class BatchChangeServlet extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static  Pattern pp = Pattern.compile("<p\\s+[a-zA-Z0-9\" \\.:]>",Pattern.CASE_INSENSITIVE|Pattern.MULTILINE);
	
	@Inject
	private Injector injector;
	
	
	@Inject
	protected com.google.inject.Provider<EntityManager> emp;

	
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		String action = req.getParameter("action");
		if("change-default-section".equals(action)){
			changeDefaultSection(req,res);
		}else if("section-to-tag".equals(action)){
			sectionToTag(req,res);
		}else if("filter-content".equals(action)){
			filterContent(req,res);
		}
	}

	
	@Transactional
	public void filterContent(HttpServletRequest req, HttpServletResponse res) {
		int siteid = Integer.valueOf(req.getParameter("siteid"));
		String qs = "SELECT a FROM Article AS a WHERE a.siteid = :siteid";
		Query q = emp.get().createQuery(qs);
		q.setParameter("siteid", siteid);
		
		List<Article> ars = q.getResultList();
		
		for(Article a : ars){
			String content = a.getContent();
		}
		
	}


	//http://127.0.0.1:8888/batchchange?action=section-to-tag&sectionid=9778&tag=uvuv
	@Transactional
	public void sectionToTag(HttpServletRequest req, HttpServletResponse res) throws IOException {
		int sectionid = Integer.valueOf(req.getParameter("sectionid"));
		String tagName = req.getParameter("tag");
		
		TagDao tdao = injector.getInstance(TagDao.class);
		
		Tag tag = tdao.findByName(tagName);
		
		if(tag == null){
			tag = new Tag();
			tag.setName(tagName);
			tdao.smartPersistBaseModel(tag);
		}
		
		Section section = emp.get().find(Section.class, sectionid);
		
		List<Article> ars = section.getArticles();
		int changed = 0;
		
		for(Article a : ars){
			List<Tag> tags = a.getTags();
			tags.add(tag);
			a.setTags(tags);
			emp.get().merge(a);
			changed++;
		}
		
		Writer out = res.getWriter();
		out.write(String.valueOf(changed));
		out.flush();
		
	}


	//http://127.0.0.1:8888/batchchange?action=change-default-section&sectionid=9778&dsectionid=9499
	@Transactional
	public void changeDefaultSection(HttpServletRequest req,
			HttpServletResponse res) throws IOException {
		int sectionid = Integer.valueOf(req.getParameter("sectionid"));
		int dsectionid = Integer.valueOf(req.getParameter("dsectionid"));
		
		Section section = emp.get().find(Section.class, sectionid);
		
		List<Article> ars = section.getArticles();
		int changed = 0;
		
		for(Article a : ars){
			a.setDefaultSectionId(dsectionid);
			emp.get().merge(a);
			changed++;
		}
		
		Writer out = res.getWriter();
		out.write(String.valueOf(changed));
		out.flush();
	}

}
